Pushover Agent. Added ability to have a default message. Made some stylistic changes as well.

akil harris 11 年之前
父节点
当前提交
c50b848953
共有 2 个文件被更改,包括 34 次插入7 次删除
  1. 8 7
      app/models/agents/pushover_agent.rb
  2. 26 0
      spec/models/agents/pushover_agent_spec.rb

+ 8 - 7
app/models/agents/pushover_agent.rb

@@ -3,14 +3,14 @@ module Agents
3 3
     cannot_be_scheduled!
4 4
     cannot_create_events!
5 5
 
6
-    @@api_url = 'https://api.pushover.net/1/messages.json'
6
+    API_URL = 'https://api.pushover.net/1/messages.json'
7 7
 
8 8
     description <<-MD
9 9
       The PushoverAgent receives and collects events and sends them via push notification to a user/group.
10 10
 
11 11
       **You need a Pushover API Token:** [https://pushover.net/apps/build](https://pushover.net/apps/build)
12 12
       
13
-      **Your event must provide** a `message` or `text` key that will contain the body of the notification. Pushover API has a `512` Character Limit including `title`. `message` will be truncated.
13
+      **You must provide** a `message` or `text` key that will contain the body of the notification. This can come from an event or be set as a default. Pushover API has a `512` Character Limit including `title`. `message` will be truncated.
14 14
 
15 15
       * `token`: your application's API token
16 16
       * `user`: the user or group key (not e-mail address).
@@ -37,6 +37,7 @@ module Agents
37 37
       {
38 38
         'token' => 'vKxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
39 39
         'user' => 'Fjxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
40
+        'message' => 'a default message',
40 41
         'device' => '',
41 42
         'title' => '',
42 43
         'url' => '',
@@ -50,15 +51,15 @@ module Agents
50 51
     end
51 52
 
52 53
     def validate_options
53
-      unless options['token'].present? && options['user'].present? && options['expected_receive_period_in_days'].present?
54
-        errors.add(:base, 'token, user, and expected_receive_period_in_days are all required.')
54
+      unless options['token'].present? && options['user'].present? && options['message'].present? && options['expected_receive_period_in_days'].present?
55
+        errors.add(:base, 'token, user, message, and expected_receive_period_in_days are all required.')
55 56
       end
56 57
     end
57 58
 
58 59
     def receive(incoming_events)
59 60
       incoming_events.each do |event|
60
-        message = (event.payload['message'] || event.payload['text']).to_s
61
-        if message != ""
61
+        message = (event.payload['message'] || event.payload['text'] || options['message']).to_s
62
+        if message.present?
62 63
             post_params = {
63 64
               'token' => options['token'],
64 65
               'user' => options['user'],
@@ -98,7 +99,7 @@ module Agents
98 99
     end
99 100
 
100 101
     def send_notification(post_params)
101
-      response = HTTParty.post(@@api_url, :query => post_params)
102
+      response = HTTParty.post(API_URL, :query => post_params)
102 103
       puts response
103 104
     end
104 105
 

+ 26 - 0
spec/models/agents/pushover_agent_spec.rb

@@ -5,6 +5,7 @@ describe Agents::PushoverAgent do
5 5
     @checker = Agents::PushoverAgent.new(:name => 'Some Name',
6 6
                                        :options => { :token => 'x',
7 7
                                                 :user => 'x',
8
+                                                :message => 'Some Message',
8 9
                                                 :device => 'Some Device',
9 10
                                                 :title => 'Some Message Title',
10 11
                                                 :url => 'http://someurl.com',
@@ -46,6 +47,26 @@ describe Agents::PushoverAgent do
46 47
       @sent_notifications[2]['message'].should == 'Some other message'
47 48
     end
48 49
 
50
+    it 'should make sure event message overrides default message' do
51
+      event = Event.new
52
+      event.agent = agents(:bob_rain_notifier_agent)
53
+      event.payload = { :message => 'Some new message'}
54
+      event.save!
55
+
56
+      @checker.receive([event])
57
+      @sent_notifications[0]['message'].should == 'Some new message'
58
+    end
59
+
60
+    it 'should make sure event text overrides default message' do
61
+      event = Event.new
62
+      event.agent = agents(:bob_rain_notifier_agent)
63
+      event.payload = { :text => 'Some new text'}
64
+      event.save!
65
+
66
+      @checker.receive([event])
67
+      @sent_notifications[0]['message'].should == 'Some new text'
68
+    end
69
+
49 70
     it 'should make sure event title overrides default title' do
50 71
       event = Event.new
51 72
       event.agent = agents(:bob_rain_notifier_agent)
@@ -158,6 +179,11 @@ describe Agents::PushoverAgent do
158 179
       @checker.should_not be_valid
159 180
     end
160 181
 
182
+    it "should validate presence of message" do
183
+      @checker.options[:message] = ""
184
+      @checker.should_not be_valid
185
+    end
186
+
161 187
     it "should validate presence of expected_receive_period_in_days" do
162 188
       @checker.options[:expected_receive_period_in_days] = ""
163 189
       @checker.should_not be_valid